// pages/my/auth/perAuth/perAuth.js
var util = require('../../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',// 登录用户信息
    auth_name: '',// 姓名
    auth_code: '',// 身份证号
    bank_account: '',// 银行卡号
    modalIsHidden: true,// 验证码弹框状态
    smsCode: '',// 短信验证码
    disabled: false,// 防止重复点击
  },

  auth_name: function (e) {
    this.setData({
      auth_name: e.detail.value
    })
  },

  auth_code: function (e) {
    this.setData({
      auth_code: e.detail.value
    })
  },

  bank_account: function (e) {
    this.setData({
      bank_account: e.detail.value
    })
  },

  smsCode: function (e) {
    this.setData({
      smsCode: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        var data = res.data;
        that.setData({
          userInfo: data
        });
        // 是否已认证
        that.getAuthData(data.user_id,data.source);
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  /**
   * 查询认证信息
   */
  getAuthData: function(userId,source){
    var that = this; 
    util.getReq('/app/sign/getPerAuthInfo', { uid: userId, source: source}, function (data) {
      if(data.code == 0){
        if (data.data != null){
          that.setData({
            authInfo: data.data
          });
        }
      }else{
        util.isError(data.desc);
      }
    });
  },

  /**
   * 新增个人认证信息
   */
  onAuthInfo: function(){
    var that = this;
    var data = that.data;
    var source = data.userInfo.source;
    var userId = data.userInfo.user_id;// 用户ID
    var auth_name = data.auth_name;// 认证人姓名
    var phone_num = data.userInfo.phone_num;// 电话
    var auth_code = data.auth_code;// 身份证号
    var bank_account = data.bank_account;// 银行卡号
    if (!auth_name){
      util.isError('请输入认证人姓名');
      return;
    }
    if (!auth_code) {
      util.isError('请输入认证人身份证号');
      return;
    }
    if (!bank_account) {
      util.isError('请输入认证人银行卡号');
      return;
    }
    that.setData({ disabled: true });
    util.getReq('/app/sign/addPerAuth',
      {
        'uid': userId,
        'source': source,
        'auth.auth_name': auth_name,
        'auth.phone_num': phone_num,
        'auth.auth_code': auth_code,
        'auth.bank_account': bank_account
      }, function (data) {
        if (data.code == 0) {
          var serviceId = data.params.service_id;
          var authId = data.params.auth_id;
          that.setData({
            serviceId: serviceId,
            authId: authId,
            modalIsHidden: false
          });
        } else {
          util.isError(data.desc);
        }
    });
    that.setData({ disabled: false });
  },

  /**
   * 发送验证码
   */
  onAuthSend: function(){
    wx.showLoading({ title: '验证码发送中' })
    var that = this;
    that.setData({ disabled: true });
    var userId = that.data.userInfo.user_id;
    var authInfo = that.data.authInfo;
    var authId = that.data.authInfo.auth_id;
    util.getReq('/app/sign/perAuthSendCode', { uid: userId, 'auth.auth_id': authId }, function (data) {
      wx.hideLoading();
      if (data.code == 0) {
        var serviceId = data.params.service_id;
        that.setData({
          serviceId: serviceId,
          modalIsHidden: false
        });
      } else {
        util.isError(data.desc);
      }
    });
    that.setData({ disabled: false });
  },

  /**
   * 短信验证码校验
   */
  confirm: function(){
    wx.showLoading({title: '认证中,请稍后...'})
    var that = this;
    var userId = that.data.userInfo.user_id;
    var authInfo = that.data.authInfo;
    var authId;
    if (!authInfo) {
      authId = that.data.authId;
    } else {
      authId = authInfo.auth_id;
    }
    var serviceId = that.data.serviceId;
    var smsCode = that.data.smsCode;
    util.getReq('/app/sign/checkPerAuthCode', { uid: userId, 'auth.auth_id': authId, service_id: serviceId, code: smsCode }, function (data) {
      wx.hideLoading();
      if (data.code == 0) {
        //隐藏验证框
        that.setData({modalIsHidden: true});
        wx.showToast({
          title: '认证成功',
          icon: 'success',
          duration: 2000
        });
        //刷新页面
        that.onLoad();
      } else {
        if (data.code == 260023 || data.code == 260051) {
          wx.showModal({
            title: '提示',
            content: '验证次数超过上限，重新发送验证码？',
            success: function (res) {
              if (res.confirm) {
                that.onAuthSend();
              } else if (res.cancel) {
                wx.showToast({
                  title: '已取消认证',
                  icon: 'success',
                  duration: 2000
                })
                that.setData({ modalIsHidden: true });
                that.onLoad();
              }
            }
          })
          return;
        }
        util.isError(data.desc);
      }
    });
  },

  /**
   * 取消
   */
  cancel: function(){
    var that = this;
    that.setData({
      modalIsHidden: true
    });
    wx.showToast({
      title: '已取消认证',
      icon: 'success',
      duration: 2000
    })
    that.onLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})